package com.ruoyi.project.modules.frontInterfaceFieldSource.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.modules.fileInfo.domain.FileInf;
import com.ruoyi.project.modules.fileInfo.mapper.FileInfoMapper;
import com.ruoyi.project.modules.interfaceinfo.domain.InterfaceInfo;
import com.ruoyi.project.modules.interfaceinfo.mapper.InterfaceInfoMapper;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.modules.frontInterfaceFieldSource.mapper.FrontInterfaceFieldSourceMapper;
import com.ruoyi.project.modules.frontInterfaceFieldSource.domain.FrontInterfaceFieldSource;
import com.ruoyi.project.modules.frontInterfaceFieldSource.service.IFrontInterfaceFieldSourceService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 前端接口字段来源Service业务层处理
 *
 * @author smallrain
 * @date 2022-05-03
 */
@Service
public class FrontInterfaceFieldSourceServiceImpl implements IFrontInterfaceFieldSourceService {
    @Resource
    private FrontInterfaceFieldSourceMapper frontInterfaceFieldSourceMapper;
    @Autowired
    private ISysDictTypeService sysDictTypeService;
    @Resource
    private InterfaceInfoMapper interfaceInfoMapper;
    @Resource
    private FileInfoMapper fileInfoMapper;

    /**
     * 查询前端接口字段来源
     *
     * @param id 前端接口字段来源主键
     * @return 前端接口字段来源
     */
    @Override
    public FrontInterfaceFieldSource selectFrontInterfaceFieldSourceById(Long id) {
        return frontInterfaceFieldSourceMapper.selectFrontInterfaceFieldSourceById(id);
    }

    /**
     * 查询前端接口字段来源列表
     *
     * @param frontInterfaceFieldSource 前端接口字段来源
     * @return 前端接口字段来源
     */
    @Override
    public List<FrontInterfaceFieldSource> selectFrontInterfaceFieldSourceList(FrontInterfaceFieldSource frontInterfaceFieldSource) {
        return frontInterfaceFieldSourceMapper.selectFrontInterfaceFieldSourceList(frontInterfaceFieldSource);
    }

    /**
     * 新增前端接口字段来源
     *
     * @param frontInterfaceFieldSource 前端接口字段来源
     * @return 结果
     */
    @Override
    public int insertFrontInterfaceFieldSource(FrontInterfaceFieldSource frontInterfaceFieldSource) {
        frontInterfaceFieldSource.setCreateTime(DateUtils.getNowDate());
        //updateAliasName(frontInterfaceFieldSource);
        return frontInterfaceFieldSourceMapper.insertFrontInterfaceFieldSource(frontInterfaceFieldSource);
    }

    private void updateAliasName(FrontInterfaceFieldSource frontInterfaceFieldSource) {
        //644
        if (frontInterfaceFieldSource.getInterfaceId() != null
                && "0".equals(frontInterfaceFieldSource.getContractValueType())
                && StringUtils.isNotEmpty(frontInterfaceFieldSource.getDictType())) {
            FileInf fileInf = new FileInf();
            fileInf.setImgType(null);
            fileInf.setIsMaster(null);
            fileInf.setId(frontInterfaceFieldSource.getPageImgId());
            if ("2".equals(frontInterfaceFieldSource.getDictCategory())) {
                List<SysDictData> sysDictData = this.sysDictTypeService.selectDictDataByType(frontInterfaceFieldSource.getDictType());
                InterfaceInfo interfaceInfo = this.interfaceInfoMapper.selectInterfaceInfoById(frontInterfaceFieldSource.getInterfaceId());
                SysDictType sysDictType = this.sysDictTypeService.selectDictTypeByType(frontInterfaceFieldSource.getDictType());
                if (sysDictType != null) {
                    String aliasName = sysDictType.getDictName() + "(" + frontInterfaceFieldSource.getDictType() + ")" + interfaceInfo.getInterfaceName() +
                            "(" + interfaceInfo.getInterfaceUrl() + ")";
                    if (CollectionUtil.isNotEmpty(sysDictData)) {
                        String str = "约定值范围是(";
                        for (int i = 0; i < sysDictData.size(); i++) {
                            SysDictData sysDictDatum = sysDictData.get(i);
                            if (sysDictDatum.getDictValue().equals(frontInterfaceFieldSource.getDictValue())) {
                                aliasName = aliasName + " 默认值是" + sysDictDatum.getDictLabel() + "(" + frontInterfaceFieldSource.getDictValue() + ")";
                            }
                            str = str + sysDictDatum.getDictLabel() + "-" + sysDictDatum.getDictValue();
                            if (i < sysDictData.size() - 1) {
                                str = str + ",";
                            }
                        }
                        aliasName = aliasName + str + ")";
                        fileInf.setAliasName(aliasName);
                        this.fileInfoMapper.updateFileInfo(fileInf);
                        return;
                    }
                }
            }
            fileInf.setAliasName("");
            this.fileInfoMapper.updateFileInfo(fileInf);
        }
    }

    /**
     * 修改前端接口字段来源
     *
     * @param frontInterfaceFieldSource 前端接口字段来源
     * @return 结果
     */
    @Override
    public int updateFrontInterfaceFieldSource(FrontInterfaceFieldSource frontInterfaceFieldSource) {
        //updateAliasName(frontInterfaceFieldSource);
        return frontInterfaceFieldSourceMapper.updateFrontInterfaceFieldSource(frontInterfaceFieldSource);
    }

    /**
     * 批量删除前端接口字段来源
     *
     * @param ids 需要删除的前端接口字段来源主键
     * @return 结果
     */
    @Override
    public int deleteFrontInterfaceFieldSourceByIds(String ids) {
        return frontInterfaceFieldSourceMapper.deleteFrontInterfaceFieldSourceByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除前端接口字段来源信息
     *
     * @param id 前端接口字段来源主键
     * @return 结果
     */
    @Override
    public int deleteFrontInterfaceFieldSourceById(Long id) {
        return frontInterfaceFieldSourceMapper.deleteFrontInterfaceFieldSourceById(id);
    }

    @Override
    public List<SysDictType> queryDictType() {
        return frontInterfaceFieldSourceMapper.queryDictType();
    }

    @Override
    public void deleteByPageImgId(Long pageImgId) {
        this.frontInterfaceFieldSourceMapper.deleteByPageImgId(pageImgId);
    }
}
